<?php
ActiveMongo::connect(sfConfig::get('app_mongo_db'), sfConfig::get('app_mongo_host'), sfConfig::get('app_mongo_username'), sfConfig::get('app_mongo_password'));
class Service extends ActiveMongo
{
    public $user;

    public $rss;

    final function getCollectionName()
    {
        return 'services';
    }

    final function before_filter($obj)
    {
        $obj['type'] = get_class($this);
    }

    function after_update($document)
    {
        if (isset($GLOBALS['debug'])) {
            var_dump(array(get_class($this) => $document));
        }
    }

    function user_filter(&$value, $old_value)
    {
    }

}

class Twitter extends  Service
{
}

class Blog extends Service
{
}

class User extends ActiveMongo
{
    public $username;
    public $password;

    public $services;

    function password_filter(&$value, $old_value)
    {
        if (strlen($value) < 5) {
            throw new ActiveMongo_FilterException("Password is too short");
        }
        $value = sha1($value);
    }

    function after_update($document)
    {
        if (isset($GLOBALS['debug'])) {
            var_dump(array(get_class($this) => $document));
        }
    }

    function username_filter($value, $old_value)
    {
        if ($old_value!=null && $value != $old_value) {
            throw new ActiveMongo_FilterException("The username can't be changed");
        }

        if (!preg_match("/[a-z][a-z0-9\-\_]+/", $value)) {
            throw new ActiveMongo_FilterException("The username is not valid");
        }

        if (strlen($value) < 5) {
            throw new ActiveMongo_FilterException("Username too short");
        }
        return TRUE;
    }

}

//User::drop();
//Service::drop();

/* Create an user for our 'aggregator' */
$user = new User;
$user->username = "crodas";
$user->password = "crodas";
$user->save();

/* Now we are going to query for the services for  
 * the new user, it will return an empty
 * dataset, but we're going to create a 'dynamic' reference
 * to the 'query'. When this reference is deference
 * then same 'query' is going to be asked to the database.
 */
$service = new Service;
$service->where('user', $user->getID())->doQuery();

/* save reference */
$user->services = $service->getReference(TRUE);
$user->save();


/* Create one service, note that Service::user is $user->getID() */
$twt = new Twitter;
$twt->user = $user->getID();
$twt->rss  = "http://twitter.com/statuses/user_timeline/crodas.rss";
$twt->save();

/* Create another service */
$blg = new Blog;
$blg->user = $user->getID();
$blg->rss  = "http://crodas.org/feed/rss";
$blg->save();

/* Create another service */
$blg1 = new Blog;
$blg1->user = $user->getID();
$blg1->rss  = "http://crodas.org/feed/rss";
$blg1->save();

/* Delete current objects */
unset($user, $blg, $blg1, $twt);

/* Output the document that is going to be sent to MongoDB */
$debug=TRUE;

$users = new User;
foreach ($users->where('username', 'crodas') as $user) {
    /* Load all references */
    $user->doDeferencing();

    /* Modify the first service */
    $user->services[0]->title = 'English Blog';

    $user->services[0]->save();
    /* You need to save the referenced document */
    /* explicitly */
    $user->services[0]->save();

    /* Modify the current user */
    $user->foobar = 'cesar';
    $user->Save();
}

//class User extends ActiveMongo
//{
//    public $username;
//    public $password;
//
//    public $services;
//
//    function password_filter(&$value, $old_value)
//    {
//        if (strlen($value) < 5) {
//            throw new ActiveMongo_FilterException("Password is too short");
//        }
//        $value = sha1($value);
//    }
//
//    function after_update($document)
//    {
//        if (isset($GLOBALS['debug'])) {
//            var_dump(array(get_class($this) => $document));
//        }
//    }
//
//    function username_filter($value, $old_value)
//    {
//        if ($old_value!=null && $value != $old_value) {
//            throw new ActiveMongo_FilterException("The username can't be changed");
//        }
//
//        if (!preg_match("/[a-z][a-z0-9\-\_]+/", $value)) {
//            throw new ActiveMongo_FilterException("The username is not valid");
//        }
//
//        if (strlen($value) < 5) {
//            throw new ActiveMongo_FilterException("Username too short");
//        }
//        return TRUE;
//    }
//
//    function add_service(Service $obj)
//    {
//        $this->services[] = $obj;
//    }
//}
//
//
//abstract class Service extends ActiveMongo
//{
//    public $user;
//
//    public $rss;
//
//
//    function user_filter(&$value, $old_value)
//    {
//    }
//
//    function after_update($document)
//    {
//        if (isset($GLOBALS['debug'])) {
//            var_dump(array(get_class($this) =>  $document));
//        }
//    }
//}
//
//class Twitter extends  Service
//{
//}
//
//class Blog extends Service
//{
//}
//
//$user = new User;
//$user->username = "crodas";
//$user->password = "crodas";
//$user->save();
//
///* Create one service */
//$twt = new Twitter;
//$twt->user = $user;
//$twt->rss  = "http://twitter.com/statuses/user_timeline/crodas.rss";
//$twt->save();
//
///* Create another service */
//$blg = new Blog;
//$blg->user = $user;
//$blg->rss  = "http://crodas.org/feed/rss";
//$blg->save();
//
///* Create another service */
//$blg1 = new Blog;
//$blg1->user = $user;
//$blg1->rss  = "http://crodas.org/feed/rss";
//$blg1->save();
//
///* Add references to the current user to its services */
//$user->add_service($blg);
//$user->add_service($blg1);
//$user->add_service($twt);
//$user->save();
//
///* Delete current objects */
//unset($user, $blg, $blg1, $twt);
//
///* Output the document that is going to be sent to MongoDB */
//$debug=TRUE;
//
//$users = new User;
//foreach ($users->where('username', 'crodas') as $user) {
//    /* Load all references */
//    $user->doDeferencing();
//
//    /* Modify the first service */
//    $user->services[0]->title = 'English Blog';
//
//    /* You need to save the referenced document */
//    /* explicitly */
//    $user->services[0]->save();
//
//    /* Modify the current user */
//    $user->foobar = 'cesar';
//    $user->Save();
//}




